<?php
namespace app\route;
use think\Log;
use think\Route;
use think\Config;
use think\DB;
use think\Request;

$mipInfo = config('mipInfo');
$request = Request::instance();

if (!strpos($request->url(),'Api')) {
    if ( strpos($request->url(),'/sitemap.xml') === 0 || strpos($request->url(),'/robots.txt') === 0
        || strpos($request->url(),'/xml/') === 0 ||strpos($request->url(),'/tagXml/') === 0
        || strpos($request->url(),'/baiduSitemapPc.xml') === 0 || strpos($request->url(),'/mainmenu.xml') === 0
        || strpos($request->url(),'/pcXml/') === 0) {
        Route::rule(['/sitemap.xml' => 'index/Spider/sitemap', '/robots.txt' => 'index/Spider/robots_txt'
            , '/xml/:page' => ['index/Spider/xml', ['ext' => 'xml'], ['page' => '\d+']]
            , '/tagXml/:id' => ['index/Spider/tagXml?id=:id', ['ext' => 'xml'], ['id' => '\d+']]
            , '/baiduSitemapPc.xml' => 'index/Spider/baiduSitemapPc'
            , '/mainmenu.xml' => 'index/Spider/mainmenu'
            , '/pcXml/:id' => ['index/Spider/pcXml?id=:id', ['ext' => 'xml'], ['id' => '\d+'] ] ]);

    }else{

        // 读取当前模板配置的路由
        $templateFilename = $mipInfo['template'];
        $tmplInfo = model('app\setting\model\Template')->getInfoByFileName($templateFilename);
        $tmplRouteTemplates =get_object_vars($tmplInfo['info']->route_template);

        // 处理模板文件中的路由
        foreach ($tmplRouteTemplates as $k=>$v){

            $dotpos = strrpos($k,'.');
            if($dotpos === false){
                Route::rule($k,'index/Index/index?template='.$v);
            }else{

                $rule = substr($k,0,$dotpos);
                $ext = substr($k,$dotpos+1);

                Route::rule([$rule => ['index/Index/index?template='.$v,['ext'=>$ext]] ] );
            }
        }

    }

}